import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import axios from 'axios'
import { forEach, set } from 'lodash'
import { formatTree } from '@/utils/util'

const path = window.serverUrl.routerPath || 'http://localhost:8080'

export default new Vuex.Store({
  state: {
    baseCode: '', // 选中行的人员uuid
    baseName: '', // 选中行的人员名
    personnelStatus: '', // 人员状态
    baseCodes: [], // 选中批量操作的人员uuid
    updateSubset: false, // 批量操作更新
    tableName: '', // 批量操作更新的子集名

    degree: false,
    wageChange: false,
    administrative: false,
    political: false,
    technical: false,
    workerLevel: false,
    wage: false,
    position: false,
    specialTalents: false,
    resume: false,
    occupation: false,
    education: false,
    reward: false,
    penalty: false,
    familySociety: false,
    liquidity: false,
    labor: false,
    cadre: false,
    retirement: false,
    investigation: false,
    concurrent: false,
    goingAbroad: false,
    RouterList: [],
    btnList: [],
  },
  actions: {
    fetchMenu({ commit }, params) {
      axios({
        method: 'GET',
        params,
        url: path + '/qxcd/caiDanGuanLi/findMenus',
      }).then((response) => {
        if (response) {
          const { data = [] } = response.data
          const { parent: RouterList, btnArr: btnList } = formatTree(data)
          sessionStorage.setItem('store', JSON.stringify({ RouterList }))
          commit('save', { RouterList, btnList })
        }
      })
    },
  },
  getters: {
    // 相当于computed
    getBaseCode(state) {
      return state.baseCode
    },
    getBaseName(state) {
      return state.baseName
    },

    getPersonnelStatus(state) {
      return state.personnelStatus
    },

    getBaseCodes(state) {
      return state.baseCodes
    },

    getUpdateSubset(state) {
      return state.updateSubset
    },

    getTableName(state) {
      return state.tableName
    },

    getDegree(state) {
      return state.degree
    },
    getWageChange(state) {
      return state.wageChange
    },
    getAdministrative(state) {
      return state.administrative
    },
    getPolitical(state) {
      return state.political
    },
    getTechnical(state) {
      return state.technical
    },
    getWorkerLevel(state) {
      return state.workerLevel
    },
    getWage(state) {
      return state.wage
    },
    getPosition(state) {
      return state.position
    },
    getSpecialTalents(state) {
      return state.specialTalents
    },
    getSpecialResume(state) {
      return state.resume
    },
    getOccupation(state) {
      return state.occupation
    },
    getEducation(state) {
      return state.education
    },
    getReward(state) {
      return state.reward
    },
    getPenalty(state) {
      return state.penalty
    },
    getFamilySociety(state) {
      return state.familySociety
    },
    getLiquidity(state) {
      return state.liquidity
    },
    getLabor(state) {
      return state.labor
    },
    getCadre(state) {
      return state.cadre
    },
    getRetirement(state) {
      return state.retirement
    },
    getInvestigation(state) {
      return state.investigation
    },
    getConcurrent(state) {
      return state.concurrent
    },
    getGoingAbroad(state) {
      return state.goingAbroad
    },
  },
  mutations: {
    // 这个比较特殊，这个里面的方法是用来同步提交到state中的。也就是说，当一个组件想要改变state中的值的话，
    // 必须通过mutations里面的方法才可以！
    // 这里会有两个参数，一个同样是state对象，另一个是传过来的参数
    changeBaseCode(state, baseCode) {
      state.baseCode = baseCode
    },
    changeBaseName(state, name) {
      state.baseName = name
    },
    changePersonnelStatus(state, personnelStatus) {
      state.personnelStatus = personnelStatus
    },

    changeBaseCodes(state, baseCodes) {
      state.baseCodes = baseCodes
    },

    changeUpdateSubset(state, updateSubset) {
      state.updateSubset = updateSubset
    },

    changeTableName(state, tableName) {
      state.tableName = tableName
    },

    changeDegree(state, degree) {
      state.degree = degree
    },
    changeWageChange(state, wageChange) {
      state.wageChange = wageChange
    },
    changeAdministrative(state, administrative) {
      state.administrative = administrative
    },
    changePolitical(state, political) {
      state.political = political
    },
    changeTechnical(state, technical) {
      state.technical = technical
    },
    changeWorkerLevel(state, workerLevel) {
      state.workerLevel = workerLevel
    },
    changeWage(state, wage) {
      state.wage = wage
    },
    changePosition(state, position) {
      state.position = position
    },
    changeSpecialTalents(state, specialTalents) {
      state.specialTalents = specialTalents
    },
    changeResume(state, resume) {
      state.resume = resume
    },
    changeOccupation(state, occupation) {
      state.occupation = occupation
    },
    changeEducation(state, education) {
      state.education = education
    },
    changeReward(state, reward) {
      state.reward = reward
    },
    changePenalty(state, penalty) {
      state.penalty = penalty
    },
    changeFamilySociety(state, familySociety) {
      state.familySociety = familySociety
    },
    changeLiquidity(state, liquidity) {
      state.liquidity = liquidity
    },
    changeLabor(state, labor) {
      state.labor = labor
    },
    changeCadre(state, cadre) {
      state.cadre = cadre
    },
    changeRetirement(state, retirement) {
      state.retirement = retirement
    },
    changeInvestigation(state, investigation) {
      state.investigation = investigation
    },
    changeConcurrent(state, concurrent) {
      state.concurrent = concurrent
    },
    changeGoingAbroad(state, goingAbroad) {
      state.goingAbroad = goingAbroad
    },
    save(state, params) {
      forEach(params, (v, k) => set(state, k, v))
    },
  },
})
